package cn.bone.motan.demo1.web;

import cn.bone.motan.demo1.api.Hello;
import cn.bone.motan.demo1.bean.Motan;
import cn.bone.motan.demo1.dao.MotanDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Random;

/**
 * Created by MD-01 on 2017/4/21.
 */
public class HelloThread implements Runnable{
    private Logger logger = LoggerFactory.getLogger(HelloThread.class);
    private int index;
    private Hello hello;
    private MotanDao motanDao;
    public HelloThread(int index,Hello hello,MotanDao motanDao){
        this.index = index;
        this.hello = hello;
        this.motanDao = motanDao;
    }
    @Override
    public void run() {
        Random random = new Random();
        while(true){
            long start = System.currentTimeMillis();
            int code = 0;
            Motan motan = new Motan();
            motan.setCreateTime(new Date());
            try{
                int a = random.nextInt(100);
                int b = random.nextInt(100);
                int c = hello.compute(a,b);
                logger.info("index:"+index);
            }catch (Exception e){
                code = 1;
                motan.setExceptionClass(e.getClass().getName());
                motan.setExceptionMessage(e.getMessage());
            }
            long end = System.currentTimeMillis();
            motan.setCode(code);
            motan.setTimeConsume(end-start);
            motanDao.add(motan);
        }
    }
}
